#include "StdAfx.h"
#include "SvrNetRPSession.h"
#include "TGSvrNetMan.h"

void SvrNetRPSession::ReInitialize()
{
	TG_AUTO_LOCK  xLock(&m_xPacketSect);
	NET_PACKET_NODE *pPacketList = (NET_PACKET_NODE*)m_xSendBlock.pBuffer;
	UINT nLast = m_nPacketListCount - 1;
	m_pPacketNodeList  = pPacketList;
	m_pWritePacketNode = pPacketList;
	m_pWritePacketNode->nWriteFlag = 1;

	m_pLastVKeyPacketNode  = NULL;
	m_pLastV2KeyPacketNode = NULL;
	m_pLastV3KeyPacketNode = NULL;
}

void  SvrNetRPSession::SetAnchorInfo(TG_ANCHOR_INFO *pAnchorInfo)
{
	memcpy(&m_xAnchorInfo,pAnchorInfo,sizeof(TG_ANCHOR_INFO));
}
SINT8* SvrNetRPSession::GetAnchorName()
{
	return m_xAnchorInfo.szUserName;
}
DWORD  SvrNetRPSession::GetAnchorID()
{
	return m_xAnchorInfo.dwUserID;
}


BOOL SvrNetRPSession::RealDataCallBack(void *pBufferData, UINT32 nBufSize, UINT32 nContext, void *pContext)
{
	if( NULL != pContext )
	{
		return ( (SvrNetRPSession*)pContext )->SetFrame( nContext,pBufferData,nBufSize);
	}
	return FALSE;
}
BOOL	SvrNetRPSession::SetFrame(DWORD dwAnchorID,void *pFrame,UINT32 nFrameSize)
{
	if (m_xAnchorInfo.dwUserID == dwAnchorID)
	{
		SetFrame(pFrame,nFrameSize);
	}
	return TRUE ;
}
DWORD  SvrNetRPSession::StartRealStream(SOCKET hSocket,UINT16 nLinkProtocol)
{  
 	switch( nLinkProtocol )
	{
	case TG_NET_PROTOCOL_TCP:
		{
			m_pRealStream = new RealStreamTcp;
		}
		break;
	case TG_NET_PROTOCOL_UDP:
		{
			m_pRealStream = new CRealStreamUdp;
		}
		break;
	default:
		break;
	}

	if( NULL == m_pRealStream )
	{
		return TG_ERR_FAILURE;
	}

	TGERR_CODE errCode = TG_ERR_SUCCESS;

	TG_REALSTREAM_INFO xRealStreamInfo ;
	xRealStreamInfo.funCallBack = RealDataCallBack;
	xRealStreamInfo.nContext    = m_xAnchorInfo.dwUserID;
	xRealStreamInfo.pContext    = (void*)this ;
	xRealStreamInfo.xConnectInfo.nLinkProtocol = nLinkProtocol ;
	xRealStreamInfo.xConnectInfo.nLinkType     = TG_NET_LINK_VA ;
	xRealStreamInfo.xConnectInfo.nNetBWState   = Net_BW_State_1 ;
	xRealStreamInfo.xConnectInfo.nNetBWCodeRate= Net_BW_CodeRate1;
	xRealStreamInfo.xConnectInfo.nTimeOut	   = 6000 ;
	
	errCode = (TGERR_CODE)m_pRealStream->StartRealStream(hSocket,&xRealStreamInfo);
	if( TG_ERR_SUCCESS != errCode )
	{
		delete m_pRealStream;
		m_pRealStream = NULL;
	}
	return  errCode ;
}